package com.sex.bull.melancholy.service.outside;

import com.google.gson.reflect.TypeToken;
import com.sex.bull.melancholy.service.Const;
import com.sex.bull.melancholy.service.bean.ModifyData;
import com.sex.bull.melancholy.service.bean.customer.CustomerPropertyDetail;
import com.sex.bull.melancholy.service.bean.customer.CustomerPropertyDetailDataSets;
import com.sex.bull.melancholy.service.bean.customer.CustomerPropertyDetailModify;
import com.sex.bull.melancholy.service.bean.customer.CustomerPropertyDict;
import com.sex.bull.melancholy.service.mybatis.bean.DetailCustomerProperty;
import com.sex.bull.melancholy.service.mybatis.bean.DictCustomerProperty;
import com.sex.bull.melancholy.service.mybatis.service.DetailCustomerPropertyService;
import com.sex.bull.melancholy.service.mybatis.service.DictCustomerPropertyService;
import com.sex.bull.melancholy.service.service.CommonService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.JsonUtils;

import java.util.*;

/**
 * Created by LiM on 2017/8/9.
 */
@Service
public class customerInfoService {
    @Autowired
    DictCustomerPropertyService dictCustomerPropertyService;
    @Autowired
    DetailCustomerPropertyService detailCustomerPropertyService;
    @Autowired
    CommonService commonService;

    /**
     * 獲取字典方式1
     *
     * @param iType
     * @return
     */
    private Map<Integer, String> getDictPropertyMapById(int iType) {
        Map<Integer, String> mReturn = new HashMap<>();
        DictCustomerProperty dictCustomerProperty = new DictCustomerProperty();
        dictCustomerProperty.setIsDelete((byte) 0);
        try {
            List<DictCustomerProperty> dictCustomerPropertyList = dictCustomerPropertyService.queryAllList(dictCustomerProperty);
            if (dictCustomerPropertyList != null && dictCustomerPropertyList.size() > 0) {
                for (DictCustomerProperty dict : dictCustomerPropertyList
                        ) {
                    switch (iType) {
                        case 0: {
                            mReturn.put(dict.getId(), dict.getNameEn());
                            break;
                        }
                        case 1: {
                            mReturn.put(dict.getId(), dict.getNameCn());
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mReturn;
    }

    /**
     * 獲取字典方式2
     *
     * @return
     */
    private Map<String, Integer> getDictPropertyMapByEn() {
        Map<String, Integer> mReturn = new HashMap<>();
        DictCustomerProperty dictCustomerProperty = new DictCustomerProperty();
        dictCustomerProperty.setIsDelete((byte) 0);
        try {
            List<DictCustomerProperty> dictCustomerPropertyList = dictCustomerPropertyService.queryAllList(dictCustomerProperty);
            if (dictCustomerPropertyList != null && dictCustomerPropertyList.size() > 0) {
                for (DictCustomerProperty dict : dictCustomerPropertyList
                        ) {
                    mReturn.put(dict.getNameEn(), dict.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mReturn;
    }

    /**
     * 獲取字典方式3
     *
     * @return
     */
    private Map<String, String> getDictPropertyCnMapByEn() {
        Map<String, String> mReturn = new HashMap<>();
        DictCustomerProperty dictCustomerProperty = new DictCustomerProperty();
        dictCustomerProperty.setIsDelete((byte) 0);
        try {
            List<DictCustomerProperty> dictCustomerPropertyList = dictCustomerPropertyService.queryAllList(dictCustomerProperty);
            if (dictCustomerPropertyList != null && dictCustomerPropertyList.size() > 0) {
                for (DictCustomerProperty dict : dictCustomerPropertyList
                        ) {
                    mReturn.put(dict.getNameEn(), dict.getNameCn());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mReturn;
    }

    /**
     * 获取所有客户属性字典
     *
     * @return
     */
    public Map<String, Object> getDictCustomerProperty() {
        Map<String, Object> jsonMap = new HashMap<>();
        DictCustomerProperty dictCustomerProperty = new DictCustomerProperty();
        dictCustomerProperty.setIsDelete((byte) 0);
        try {
            List<DictCustomerProperty> dictCustomerPropertyList = dictCustomerPropertyService.queryAllList(dictCustomerProperty);
            if (dictCustomerPropertyList != null && dictCustomerPropertyList.size() > 0) {
                List<CustomerPropertyDict> customerPropertyDictList = new ArrayList<>();
                for (DictCustomerProperty dict : dictCustomerPropertyList
                        ) {
                    CustomerPropertyDict customerPropertyDict = new CustomerPropertyDict();
                    customerPropertyDict.setPropertyEN(dict.getNameEn());
                    customerPropertyDict.setPropertyCN(dict.getNameCn());
                    customerPropertyDictList.add(customerPropertyDict);
                }
                commonService.addSuccMessage(jsonMap, dictCustomerPropertyList, Const.SUCCESS_QUERY_MSG);
            } else
                commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_QUERY_NULL_CODE, Const.ERR_DICT_CUSTOMER_QUERY_NULL_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_QUERY_CODE, Const.ERR_DICT_CUSTOMER_QUERY_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 新增用户属性字典
     *
     * @param sProperty
     * @return
     */
    public Map<String, Object> addDictCustomerProperty(String sProperty) {
        Map<String, Object> jsonMap = new HashMap<>();
        List<CustomerPropertyDict> customerPropertyDictList = JsonUtils.json2Objs(sProperty,
                new TypeToken<List<CustomerPropertyDict>>() {
                });
        if (customerPropertyDictList == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }
        List<String> enList = new ArrayList<>();
        for (CustomerPropertyDict dict : customerPropertyDictList
                ) {
            enList.add(dict.getPropertyEN());
        }

        try {
            List<DictCustomerProperty> dictCustomerPropertyList = dictCustomerPropertyService.getEnHas(enList);
            if (dictCustomerPropertyList != null && dictCustomerPropertyList.size() > 0) {
                String sEnName = "";
                for (DictCustomerProperty dict : dictCustomerPropertyList
                        ) {
                    sEnName = sEnName != "" ? sEnName + "," + dict.getNameEn() : dict.getNameEn();
                }
                commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_ADD_HAS_CODE,
                        Const.ERR_DICT_CUSTOMER_ADD_HAS_MSG + sEnName);
                return jsonMap;
            }

            dictCustomerPropertyList = new ArrayList<>();
            for (CustomerPropertyDict dict : customerPropertyDictList
                    ) {
                DictCustomerProperty dictCustomerProperty = new DictCustomerProperty();
                dictCustomerProperty.setNameEn(dict.getPropertyEN());
                dictCustomerProperty.setNameCn(dict.getPropertyCN());
                dictCustomerProperty.setCreateTime(new Date());
                dictCustomerProperty.setIsDelete((byte) 0);
                dictCustomerPropertyList.add(dictCustomerProperty);
            }

            dictCustomerPropertyService.addData(dictCustomerPropertyList);
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_ADD_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_ADD_CODE, Const.ERR_DICT_CUSTOMER_ADD_MSG);
            return jsonMap;
        }

        return jsonMap;
    }

    /**
     * 更新客户属性字典表
     *
     * @param sProperty
     * @return
     */
    public Map<String, Object> updateDictCustomerProperty(String sProperty) {
        Map<String, Object> jsonMap = new HashMap<>();

        List<ModifyData> modifyDataList = JsonUtils.json2Objs(sProperty, new TypeToken<List<ModifyData>>() {
        });
        if (modifyDataList == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }

        List<String> enList = new ArrayList<>();
        for (ModifyData modifyData : modifyDataList
                ) {
            enList.add(modifyData.getPropertyName());
        }

        try {
            List<DictCustomerProperty> dictCustomerPropertyList = dictCustomerPropertyService.getEnHas(enList);
            if (dictCustomerPropertyList == null || dictCustomerPropertyList.size() == 0) {
                commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_QUERY_NULL_CODE, Const.ERR_DICT_CUSTOMER_QUERY_NULL_MSG);
                return jsonMap;
            }

            Map<String, Integer> mIds = new HashMap<>();
            for (DictCustomerProperty dict : dictCustomerPropertyList
                    ) {
                mIds.put(dict.getNameEn(), dict.getId());
            }

            if (mIds.size() != enList.size()) {
                String sEn = "";
                for (String sKey : enList
                        ) {
                    if (!mIds.containsKey(sKey))
                        sEn = sEn != "" ? sEn + "," + sKey : sKey;
                }
                commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_UPDATE_MORE_CODE,
                        Const.ERR_DICT_CUSTOMER_UPDATE_MORE_MSG);
                return jsonMap;
            }

            for (ModifyData modifyData : modifyDataList
                    ) {
                Integer iId = mIds.get(modifyData.getPropertyName());
                if (iId != null) {
                    DictCustomerProperty dictCustomerProperty = new DictCustomerProperty();
                    dictCustomerProperty.setId(iId);
                    if (modifyData.getPropertyName().toLowerCase().equals(modifyData.getOldValue().toLowerCase())) {
                        dictCustomerProperty.setNameEn(modifyData.getNewValue());
                    } else dictCustomerProperty.setNameCn(modifyData.getNewValue());
                    dictCustomerProperty.setUpdateTime(new Date());
                    //还有些问题 后期修改
                    dictCustomerPropertyService.updateData(dictCustomerProperty);
                }
            }
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_UPDATE_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_UPDATE_CODE, Const.ERR_DICT_CUSTOMER_UPDATE_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 逻辑删除客户属性字典
     *
     * @param sProperty
     * @return
     */
    public Map<String, Object> deleteDictCustomerProperty(String sProperty) {
        Map<String, Object> jsonMap = new HashMap<>();
        List<CustomerPropertyDict> customerPropertyDictList = JsonUtils.json2Objs(sProperty,
                new TypeToken<List<CustomerPropertyDict>>() {
                });
        if (customerPropertyDictList == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }
        List<String> enList = new ArrayList<>();
        for (CustomerPropertyDict dict : customerPropertyDictList
                ) {
            enList.add(dict.getPropertyEN());
        }

        try {
            dictCustomerPropertyService.deleteDataBat(enList);
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_DELETE_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DICT_CUSTOMER_DELETE_CODE, Const.ERR_DICT_CUSTOMER_DELETE_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 根據編號查詢客戶屬性
     *
     * @param lCustomerId
     * @return
     */
    public Map<String, Object> getDetailCustomerPropertyById(Long lCustomerId) {
        Map<String, Object> jsonMap = new HashMap<>();
        DetailCustomerProperty detailCustomerProperty = new DetailCustomerProperty();
        detailCustomerProperty.setIsDelete((byte) 0);
        detailCustomerProperty.setCustomerId(lCustomerId);
        try {
            List<DetailCustomerProperty> detailCustomerPropertieList = detailCustomerPropertyService.queryAllList(detailCustomerProperty);
            if (detailCustomerPropertieList != null && detailCustomerPropertieList.size() > 0) {
                Map<Integer, String> mEn = getDictPropertyMapById(0);
                List<CustomerPropertyDetail> customerPropertyDetails = new ArrayList<>();
                for (DetailCustomerProperty detail : detailCustomerPropertieList
                        ) {
                    CustomerPropertyDetail customerPropertyDetail = new CustomerPropertyDetail();
                    customerPropertyDetail.setPropertyEN(mEn.get(detail.getPropertyId()));
                    customerPropertyDetail.setPropertyValue(detail.getPropertyValue());
                    customerPropertyDetails.add(customerPropertyDetail);
                }
                CustomerPropertyDetailDataSets customerPropertyDetailDataSets = new CustomerPropertyDetailDataSets();
                customerPropertyDetailDataSets.setCustomerId(lCustomerId);
                customerPropertyDetailDataSets.setCustomerPropertyDetails(customerPropertyDetails);
                commonService.addSuccMessage(jsonMap, customerPropertyDetailDataSets, Const.SUCCESS_QUERY_MSG);
            } else
                commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_QUERY_NULL_CODE, Const.ERR_DETAIL_CUSTOMER_QUERY_NULL_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_QUERY_CODE, Const.ERR_DETAIL_CUSTOMER_QUERY_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 根據相關屬性查詢客戶信息
     *
     * @param sDetail
     * @return
     */
    public Map<String, Object> getDetailCustomerPropertyByPara(String sDetail) {
        Map<String, Object> jsonMap = new HashMap<>();
        JSONObject jsonObject = commonService.isJsonData(sDetail);
        if (jsonObject == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }

        Map<String, Integer> mEnIds = getDictPropertyMapByEn();
        List<DetailCustomerProperty> detailCustomerProperties = new ArrayList<>();
        for (String sEn : jsonObject.keySet()
                ) {
            DetailCustomerProperty detailCustomerProperty = new DetailCustomerProperty();
            detailCustomerProperty.setIsDelete((byte) 0);
            detailCustomerProperty.setPropertyId(mEnIds.get(sEn));
            detailCustomerProperty.setPropertyValue(jsonObject.getString(sEn));
            detailCustomerProperties.add(detailCustomerProperty);
        }
        try {
            List<DetailCustomerProperty> detailCustomerPropertyList = detailCustomerPropertyService.queryByPara(detailCustomerProperties);
            if (detailCustomerPropertyList != null && detailCustomerPropertyList.size() > 0) {
                Map<Integer, String> mEn = getDictPropertyMapById(0);
                Map<Long, CustomerPropertyDetailDataSets> mData = new HashMap<>();
                for (DetailCustomerProperty detail : detailCustomerPropertyList
                        ) {
                    CustomerPropertyDetailDataSets customerPropertyDetailDataSets = mData.get(detail.getCustomerId());
                    if (customerPropertyDetailDataSets != null) {
                        CustomerPropertyDetail customerPropertyDetail = new CustomerPropertyDetail();
                        customerPropertyDetail.setPropertyEN(mEn.get(detail.getPropertyId()));
                        customerPropertyDetail.setPropertyValue(detail.getPropertyValue());
                        List<CustomerPropertyDetail> customerPropertyDetailList = customerPropertyDetailDataSets.getCustomerPropertyDetails();
                        if (customerPropertyDetailList != null) {
                            customerPropertyDetailList.add(customerPropertyDetail);
                        } else {
                            customerPropertyDetailList = new ArrayList<>();
                            customerPropertyDetailList.add(customerPropertyDetail);
                            customerPropertyDetailDataSets.setCustomerPropertyDetails(customerPropertyDetailList);
                        }
                    } else {
                        CustomerPropertyDetail customerPropertyDetail = new CustomerPropertyDetail();
                        customerPropertyDetail.setPropertyEN(mEn.get(detail.getPropertyId()));
                        customerPropertyDetail.setPropertyValue(detail.getPropertyValue());
                        List<CustomerPropertyDetail> customerPropertyDetailList = new ArrayList<>();
                        customerPropertyDetailList.add(customerPropertyDetail);
                        customerPropertyDetailDataSets = new CustomerPropertyDetailDataSets();
                        customerPropertyDetailDataSets.setCustomerId(detail.getCustomerId());
                        customerPropertyDetailDataSets.setCustomerPropertyDetails(customerPropertyDetailList);
                        mData.put(detail.getCustomerId(), customerPropertyDetailDataSets);
                    }
                }
                commonService.addSuccMessage(jsonMap, mData, Const.SUCCESS_QUERY_MSG);
            } else
                commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_QUERY_NULL_CODE, Const.ERR_DETAIL_CUSTOMER_QUERY_NULL_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_QUERY_CODE, Const.ERR_DETAIL_CUSTOMER_QUERY_MSG);
            return jsonMap;
        }

        return jsonMap;
    }

    /**
     * 新增客户属性
     *
     * @param sDetail
     * @return
     */
    public Map<String, Object> addDetailCustomerProperty(String sDetail) {
        Map<String, Object> jsonMap = new HashMap<>();
        List<CustomerPropertyDetailDataSets> customerPropertyDetailDataSetsList = JsonUtils.json2Objs(sDetail,
                new TypeToken<List<CustomerPropertyDetailDataSets>>() {
                });
        if (customerPropertyDetailDataSetsList == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }
        Map<String, Integer> mIds = getDictPropertyMapByEn();
        List<DetailCustomerProperty> detailCustomerPropertyList = new ArrayList<>();
        boolean bAdd = true;
        for (CustomerPropertyDetailDataSets customerData : customerPropertyDetailDataSetsList
                ) {
            Long lCustomerId = customerData.getCustomerId();
            List<CustomerPropertyDetail> customerPropertyDetailList = customerData.getCustomerPropertyDetails();
            if (customerPropertyDetailList == null) {
                commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_ADD_NO_DATA_CODE,
                        String.valueOf(lCustomerId) + Const.ERR_DETAIL_CUSTOMER_ADD_NO_DATA_MSG);
                bAdd = false;
                break;
            }
            for (CustomerPropertyDetail customerDetail : customerPropertyDetailList
                    ) {
                DetailCustomerProperty detailCustomerProperty = new DetailCustomerProperty();
                detailCustomerProperty.setCustomerId(lCustomerId);
                detailCustomerProperty.setPropertyId(mIds.get(customerDetail.getPropertyEN()));
                detailCustomerProperty.setPropertyValue(customerDetail.getPropertyValue());
                detailCustomerProperty.setIsDelete((byte) 0);
                detailCustomerProperty.setCreateTime(new Date());
                detailCustomerPropertyList.add(detailCustomerProperty);
            }
        }
        if (bAdd) {
            try {
                detailCustomerPropertyService.addData(detailCustomerPropertyList);
                commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_ADD_MSG);
            } catch (Exception e) {
                e.printStackTrace();
                commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_ADD_CODE, Const.ERR_DETAIL_CUSTOMER_ADD_MSG);
                return jsonMap;
            }
        }
        return jsonMap;
    }

    /**
     * 更新客户属性
     *
     * @param sDetail
     * @return
     */
    public Map<String, Object> updateDetailCustomerProperty(String sDetail) {
        Map<String, Object> jsonMap = new HashMap<>();
        List<CustomerPropertyDetailModify> customerPropertyDetailModifyList = JsonUtils.json2Objs(sDetail,
                new TypeToken<List<CustomerPropertyDetailModify>>() {
                });
        if (customerPropertyDetailModifyList == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }
        Map<String, Integer> mIds = getDictPropertyMapByEn();
        try {
            for (CustomerPropertyDetailModify cModify : customerPropertyDetailModifyList
                    ) {
                Long lCustomerId = cModify.getCustomerId();
                List<ModifyData> modifyDataList = cModify.getModifyData();
                if (modifyDataList != null) {
                    for (ModifyData modify : modifyDataList
                            ) {
                        Map<String, Object> mPara = new HashMap<>();
                        mPara.put(Const.CUSTOMER_ID, lCustomerId);
                        mPara.put(Const.OLD_VALUR, modify.getOldValue());
                        mPara.put(Const.NEW_VALUE, modify.getNewValue());
                        mPara.put(Const.PROPERTY_IC, mIds.get(modify.getPropertyName()));
                        detailCustomerPropertyService.updataByMap(mPara);
                    }
                }
            }
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_UPDATE_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_UPDATE_CODE, Const.ERR_DETAIL_CUSTOMER_UPDATE_MSG);
            return jsonMap;
        }
        return jsonMap;
    }

    /**
     * 删除客户属性
     *
     * @param sDetail
     * @return
     */
    public Map<String, Object> deleteDetailCustomerProperty(String sDetail) {
        Map<String, Object> jsonMap = new HashMap<>();
        List<CustomerPropertyDetailDataSets> customerPropertyDetailDataSetsList = JsonUtils.json2Objs(sDetail,
                new TypeToken<List<CustomerPropertyDetailDataSets>>() {
                });
        if (customerPropertyDetailDataSetsList == null) {
            commonService.addErrMessage(jsonMap, Const.ERR_NOT_JSON_CODE, Const.ERR_NOT_JSON_MSG);
            return jsonMap;
        }

        Map<String, Integer> mIds = getDictPropertyMapByEn();
        try {
            for (CustomerPropertyDetailDataSets customerDetailDataSet : customerPropertyDetailDataSetsList
                    ) {
                Long lCustomerId = customerDetailDataSet.getCustomerId();
                List<CustomerPropertyDetail> customerPropertyDetailList = customerDetailDataSet.getCustomerPropertyDetails();
                if (customerPropertyDetailList != null) {
                    for (CustomerPropertyDetail cust : customerPropertyDetailList
                            ) {
                        DetailCustomerProperty detailCustomerProperty = new DetailCustomerProperty();
                        detailCustomerProperty.setCustomerId(lCustomerId);
                        detailCustomerProperty.setPropertyId(mIds.get(cust.getPropertyEN()));
                        detailCustomerProperty.setPropertyValue(cust.getPropertyValue());
                        detailCustomerPropertyService.deleteData(detailCustomerProperty);
                    }

                }
            }
            commonService.addSuccMessage(jsonMap, null, Const.SUCCESS_DELETE_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            commonService.addErrMessage(jsonMap, Const.ERR_DETAIL_CUSTOMER_DELETE_CODE, Const.ERR_DETAIL_CUSTOMER_DELETE_MSG);
            return jsonMap;
        }
        return jsonMap;
    }
}
